home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d18 / intrfc55.arc / UTIL.PAS < prev   
Pascal/Delphi Source File  |  1990-02-25  |  7KB  |  290 lines

  1. unit util;
  2.  
  3. interface
  4.   uses dos;
  5.  
  6.   var
  7.     last_file_size : longint;
  8.  
  9.   function normalize(p:pointer):pointer;
  10.  
  11.   function add_offset(p:pointer; add:word):pointer;
  12.  
  13.   function asciiz2s(var asciiz):string;
  14.  
  15.   function upper(var s:string):string;
  16.  
  17.   function ptr_diff(p1,p2:pointer):longint;
  18.  
  19.   function minw(i,j:word):word;
  20.  
  21.   function maxw(i,j:word):word;
  22.  
  23.   function minl(i,j:longint):longint;
  24.  
  25.   function maxl(i,j:longint):longint;
  26.  
  27.   function word_at(var b:byte):word;
  28.  
  29.   procedure read_file(filename: string;var buffer:pointer;
  30.                      offset:longint; size:word);
  31.   { Attempts to read a file into buffer; returns nil if there was a problem }
  32.  
  33.  
  34.   function roundup(n,r:word):word;
  35.  
  36.   procedure get_load_path(var s:string);
  37.   { Returns the path to the currently running program;  needs DOS 3+ }
  38.  
  39.   function get_unique_filename(var path:string; attr:word):word;
  40.   { Creates new file in given directory, appends name to path, returns error }
  41.  
  42.   function is_a_file(var f):boolean;
  43.   { Determines if the file in f is really a file, or is a device.
  44.     f may be either a TP file type or a DOS file handle
  45.     Assumes f is open
  46.   }
  47.   function freeheap:integer;
  48.   { Frees memory from the heap pointer up to the top of the free list
  49.     for use by other programs.  Will destroy the free list!
  50.     Returns 0 if successful, dos error code if not.  Should always
  51.     be successful?
  52.   }
  53.   function restoreheap:integer;
  54.   { Restores memory freed by freeheap.
  55.     Does not restore the free list;  will leave garbage in it.
  56.     Returns 0 if successful, dos error code if not.  Will fail if memory
  57.     is no longer free, e.g. a TSR was run in it.
  58.   }
  59.  
  60. implementation
  61.  
  62. var
  63.   regs : registers;
  64.  
  65. function normalize(p:pointer):pointer;
  66. var
  67.   s,o : word;
  68. begin
  69.   s := seg(p^);
  70.   o := ofs(p^);
  71.   if o > $f then
  72.   begin
  73.     s := s + o shr 4;
  74.     o := o and $f;
  75.   end;
  76.   normalize := ptr(s,o);
  77. end;
  78.  
  79. function add_offset(p:pointer; add:word):pointer;
  80. begin
  81.   p := normalize(p);
  82.   add_offset := ptr(seg(p^),ofs(p^)+add);
  83. end;
  84.  
  85. function asciiz2s(var asciiz):string;
  86. var a:array[0..255] of char absolute asciiz;
  87.     i:integer;
  88.     s:string;
  89. begin
  90.  i:=0;
  91.  while a[i]<>chr(0) do inc(i);
  92.  {$r-}
  93.  s[0]:=chr(i);
  94.  move(a,s[1],i);
  95.  {$r+}
  96.  asciiz2s:=s
  97. end;
  98.  
  99. function upper(var s:string):string;
  100. var
  101.   i:integer;
  102.   result : string;
  103. begin
  104.   result[0] := s[0];
  105.   for i:=1 to length(s) do
  106.     result[i] := upcase(s[i]);
  107.   upper := result;
  108. end;
  109.  
  110. function ptr_diff(p1,p2:pointer):longint;
  111. begin
  112.   ptr_diff := 16*(longint(seg(p1^))-longint(seg(p2^))) + ofs(p1^) - ofs(p2^);
  113. end;
  114.  
  115. function minw(i,j:word):word;
  116. begin
  117.   if i<j then
  118.     minw := i
  119.   else
  120.     minw := j;
  121. end;
  122.  
  123. function maxw(i,j:word):word;
  124. begin
  125.   if i<j then
  126.     maxw := j
  127.   else
  128.     maxw := i;
  129. end;
  130.  
  131. function minl(i,j:longint):longint;
  132. begin
  133.   if i<j then
  134.     minl := i
  135.   else
  136.     minl := j;
  137. end;
  138.  
  139. function maxl(i,j:longint):longint;
  140. begin
  141.   if i<j then
  142.     maxl := j
  143.   else
  144.     maxl := i;
  145. end;
  146.  
  147. function word_at(var b:byte):word;
  148. var
  149.   p:^byte;
  150. begin
  151.   p := add_offset(@b,1);
  152.   word_at := word(b) + word(p^) shl 8;
  153. end;
  154.  
  155. procedure read_file(filename: string;var buffer:pointer;
  156.                    offset:longint; size:word);
  157. { Attempts to read a file into buffer; returns nil if there was a problem }
  158. var
  159.   f:file;
  160.   try_size : longint;
  161. begin
  162.   assign(f,filename);
  163.   buffer := nil;
  164.   {$i-} reset(f,1); {$i+}
  165.   if ioresult <> 0 then
  166.     exit;
  167.   last_file_size := filesize(f);
  168.   try_size := last_file_size-offset;
  169.   if try_size < size then
  170.     size := try_size;
  171.   try_size := size;
  172.   if size > 65521 then
  173.   begin
  174.     writeln('File size too large.  File not read.');
  175.     exit;
  176.   end;
  177.   if maxavail < size then
  178.   begin
  179.     writeln('Out of memory.  File ',filename,' not read.');
  180.     exit;
  181.   end;
  182.   getmem(buffer,size);
  183.   seek(f,offset);
  184.   blockread(f,buffer^,try_size,size);
  185.   close(f);
  186. end;
  187.  
  188.  
  189. function roundup(n,r:word):word;
  190. begin
  191.   roundup := r*((n+r-1) div r);
  192. end;
  193.  
  194. procedure get_load_path(var s:string);
  195. { Returns the path to the currently running program;  needs DOS 3+ }
  196. var
  197.   p,q:pointer;
  198.   l:longint absolute p;
  199.   len:byte;
  200. begin
  201.   p := ptr(prefixseg,$2c);    { Point to environment segment number }
  202.   p := ptr(word(p^),0);       { Point to start of environment segment }
  203.   while word(p^) <> 0 do      { Find terminating double 0 }
  204.     inc(l);
  205.   inc(l,4);                   { Skip double zero and count word }
  206.  
  207.   q := p;                     { Save start of string }
  208.   len := 0;
  209.   while byte(p^) <> 0 do
  210.   begin
  211.     inc(len);
  212.     inc(l);
  213.   end;
  214.   s[0] := char(len);
  215.   move(q^,s[1],len);
  216. end;
  217.  
  218. function get_unique_filename(var path:string; attr:word):word;
  219. { Appends new name to path;  Returns error value or zero if ok }
  220. begin
  221.   path[length(path)+1] := char(0);
  222.   regs.ah := $5A;
  223.   regs.ds := seg(path[1]);
  224.   regs.dx := ofs(path[1]);
  225.   regs.cx := attr;
  226.   msdos(regs);
  227.   if ((regs.flags and fcarry) <> 0) then
  228.     get_unique_filename := regs.ax
  229.   else
  230.   begin
  231.     get_unique_filename := 0;
  232.     path := asciiz2s(path[1]);
  233.   end;
  234. end;
  235.  
  236. function is_a_file(var f):boolean;
  237. { Determines if the file in f is really a file, or is a device
  238.   Assumes f is open
  239. }
  240. var
  241.   handle : word absolute f;
  242. begin
  243.   regs.ah := $44;  { IOCTL }
  244.   regs.al :=   0;  { Get device information }
  245.   regs.bx := handle;
  246.   msdos(regs);
  247.   if (regs.flags and fcarry) <> 0 then
  248.     is_a_file := false
  249.   else
  250.     is_a_file := (regs.dx and (1 shl 7)) = 0;
  251. end;
  252.  
  253. function freeheap:integer;
  254. { Frees memory from the heap pointer up to the top of the free list
  255.   for use by other programs.  Will destroy the free list!
  256.   Returns 0 if successful, dos error code if not.  Should always
  257.   be successful?
  258. }
  259. begin
  260.   regs.ah := $4a;   { Setblock }
  261.   regs.bx := seg(heapptr^) + ofs(heapptr^) div 16 + 1 - prefixseg;
  262.   regs.es := prefixseg;
  263.   msdos(regs);
  264.   if (regs.flags and fcarry) = 0 then
  265.     freeheap := 0
  266.   else
  267.     freeheap := regs.ax;
  268. end;
  269.  
  270. function restoreheap:integer;
  271. { Restores memory freed by freeheap.
  272.   Does not restore the free list;  will leave garbage in it.
  273.   Returns 0 if successful, dos error code if not.  Will fail if memory
  274.   is no longer free, e.g. a TSR was run in it.
  275. }
  276. begin
  277.   regs.ah := $4a;   { Setblock }
  278.   regs.bx := seg(freeptr^) + $1000 - prefixseg;
  279.   regs.es := prefixseg;
  280.   msdos(regs);
  281.   if (regs.flags and fcarry) = 0 then
  282.     restoreheap := 0
  283.   else
  284.     restoreheap := regs.ax;
  285. end;
  286.  
  287. end.
  288.  
  289.  
  290.